package sk.fiit.icp.event.view;

import java.util.List;
import java.util.Set;

import org.apache.wicket.Page;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxFallbackLink;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.extensions.yui.calendar.DateTimeField;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.HiddenField;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.panel.EmptyPanel;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.request.resource.ContextRelativeResource;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.validation.validator.StringValidator;

import sk.fiit.icp.data.EventCategories;
import sk.fiit.icp.data.EventCategoriesDao;
import sk.fiit.icp.data.Events;
import sk.fiit.icp.data.EventsDao;
import sk.fiit.icp.data.Repetition;
import sk.fiit.icp.data.RepetitionDao;
import sk.fiit.icp.data.UsersDao;
import sk.fiit.icp.event.components.CategoryPage;
import sk.fiit.icp.event.components.ExamplePage;
import sk.fiit.icp.event.components.Foo;
import sk.fiit.icp.service.SessionService;

public class RegistrationForm extends Form<Events> {

	@SpringBean(name = "eventsDao")
	public EventsDao eventsDao;

	@SpringBean(name = "usersDao")
	public UsersDao usersDao;

	@SpringBean(name = "eventCategoriesDao")
	public EventCategoriesDao eventCategoriesDao;

	@SpringBean(name = "repetitionDao")
	public RepetitionDao repetitionDao;

	@SpringBean(name = "sessionService")
	SessionService sessionService;

	private int isWindow;
	private int sliderValue = 10;

	private Set<Foo> resourceReference;
	private ModalWindow modalWindow;
	private ModalWindow categoryModalWindow;

	public Set<Foo> getResourceReference() {
		return resourceReference;
	}

	public void setResourceReference(Set<Foo> resourceReference) {
		this.resourceReference = resourceReference;
	}

	public RegistrationForm(String id, CompoundPropertyModel<Events> eventModel, int window) {
		super( id, eventModel );
		setOutputMarkupId( true );
		this.isWindow = window;
		final FeedbackPanel feedbackPanel = new FeedbackPanel( "feedback" );
		feedbackPanel.setOutputMarkupId( true );
		add( feedbackPanel );

		add( new RequiredTextField<String>( "title" ).add( StringValidator.maximumLength( 20 ) ) );
		add( new DateTimeField( "startTime" ).setRequired( true ) );
		add( new DateTimeField( "endTime" ).setRequired( true ) );
		add( new TextArea<String>( "text" ) );
		add( new CheckBox( "allDayEvent" ) );
		add( new CheckBox( "archive" ) );
		add( new TextField<String>( "location" ).setRequired( true ) );

		AjaxSubmitLink link = new AjaxSubmitLink( "link" ) {
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				getModel().getObject().setEvents( sessionService.getEventParent() );
				eventsDao.persist( RegistrationForm.this.getModelObject() );
				target.appendJavaScript( "CloseModelWindow(function() {}); 	$('#gridcontainer').reload();" );
			}

			@Override
			protected void onError(AjaxRequestTarget target, Form<?> form) {
				target.add( feedbackPanel );
			}
		};

		link.add( new Image( "saveImage", new ContextRelativeResource( "/images/Page/ButtonSave.png" ) ) );
		add( link );

		add( new DropDownChoice<EventCategories>( "eventCategories", createEvenetCategoriesModel(), new ChoiceRenderer<EventCategories>(
				"name" ) ) {
			public boolean isRequired() {
				// if ( RegistrationForm.this.get( "link" ) ==
				// findSubmittingButton() ) {
				// return true;
				// }
				return true;
			};
		}.setRequired( true ) );

		add( new EmptyPanel( "categoryModalWindow" ) );
		addCategoryPanel();

		add( new DropDownChoice<Repetition>( "repetition", createEventRepetitionsModel(), new ChoiceRenderer<Repetition>( "name" ) ) {
			public boolean isRequired() {
				// if ( RegistrationForm.this.get( "link" ) ==
				// RegistrationForm.this.findSubmittingButton() ) {
				// return true;
				// }
				return true;
			};
		}.setRequired( true ) );

		add( new EmptyPanel( "modalWindow" ) );
		addEventPanel();

		// Slider slider = new Slider("slider", 0, 100);
		// slider.setValue(this.sliderValue);
		// slider.setSlideEvent(new
		// DefaultJsScopeUiEvent("$(\"#sliderValue\").val(" +
		// Slider.UI_VALUE + ");"));
		// add(slider);

		add( new HiddenField<Integer>( "sliderValue", new PropertyModel<Integer>( this, "sliderValue" ) ) );
		add( new HiddenField<Integer>( "idevent" ) );
		add( new HiddenField<Integer>( "isWindow", new PropertyModel<Integer>( this, "isWindow" ) ) );

	}

	int getProgressValue() {
		return sliderValue;
	}

	int getIsWindow() {
		return isWindow;
	}

	public void addCategoryPanel() {
		AjaxSubmitLink link = new AjaxSubmitLink( "addCategoryLink" ) {

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				categoryModalWindow = createCategoryModalWindow( "categoryModalWindow" );
				categoryModalWindow.show( target );
			}

			@Override
			protected void onError(AjaxRequestTarget target, Form<?> form) {
				target.add( form );
			}
		};
		link.add( new Image( "addCategoryImage", new ContextRelativeResource( "/images/Page/ButtonAdd.png" ) ) );
		add( link );
	}

	public void addEventPanel() {
		final Events event = RegistrationForm.this.getModelObject();

		if ( event.getEvents() != null ) {
			add( new Label( "parentEventTitle", new PropertyModel<String>( event.getEvents(), "title" ) ) );
			add( new AjaxFallbackLink<String>( "deleteEvent" ) {

				@Override
				public void onClick(AjaxRequestTarget target) {
					eventsDao.update( event );
					target.add( RegistrationForm.this );
				}
			} );

		} else {
			add( new EmptyPanel( "parentEventTitle" ) );
			add( new EmptyPanel( "deleteEvent" ) );
		}

		add( new AjaxSubmitLink( "addParentEvent" ) {

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				modalWindow = createModalWindow( "modalWindow" );
				modalWindow.show( target );
			}

			@Override
			protected void onError(AjaxRequestTarget target, Form<?> form) {
				target.add( form );
			}
		} );
	}

	public ModalWindow createModalWindow(String id) {
		final ModalWindow modal1;
		addOrReplace( modal1 = new ModalWindow( id ) );
		modal1.setOutputMarkupId( true );

		modal1.setCookieName( id );

		modal1.setPageCreator( new ModalWindow.PageCreator() {
			public Page createPage() {
				return new ExamplePage( RegistrationForm.this, modalWindow );
			}
		} );
		modal1.setWindowClosedCallback( new ModalWindow.WindowClosedCallback() {
			public void onClose(AjaxRequestTarget target) {
				target.add( RegistrationForm.this );
			}
		} );
		modal1.setCloseButtonCallback( new ModalWindow.CloseButtonCallback() {
			public boolean onCloseButtonClicked(AjaxRequestTarget target) {

				return true;
			}
		} );
		return modal1;
	}

	public ModalWindow createCategoryModalWindow(String id) {
		final ModalWindow modal2;
		addOrReplace( modal2 = new ModalWindow( id ) );
		modal2.setOutputMarkupId( true );
		modal2.setOutputMarkupPlaceholderTag( true );
		modal2.setCookieName( id );
		modal2.setAutoSize( true );
		modal2.setPageCreator( new ModalWindow.PageCreator() {
			public Page createPage() {
				return new CategoryPage( RegistrationForm.this, categoryModalWindow );
			}
		} );
		modal2.setWindowClosedCallback( new ModalWindow.WindowClosedCallback() {
			public void onClose(AjaxRequestTarget target) {
				target.add();
			}
		} );
		modal2.setCloseButtonCallback( new ModalWindow.CloseButtonCallback() {
			public boolean onCloseButtonClicked(AjaxRequestTarget target) {
				return true;
			}
		} );
		return modal2;
	}

	public void setEventParent(AjaxRequestTarget target) {
		getModel().getObject().setEvents( sessionService.getEventParent() );
		target.add( RegistrationForm.this );
	}

	public IModel<List<? extends Repetition>> createEventRepetitionsModel() {
		return new AbstractReadOnlyModel<List<? extends Repetition>>() {

			@Override
			public List<? extends Repetition> getObject() {
				return repetitionDao.findAll();
			}
		};
	}

	public IModel<List<? extends EventCategories>> createEvenetCategoriesModel() {
		return new AbstractReadOnlyModel<List<? extends EventCategories>>() {

			@Override
			public List<? extends EventCategories> getObject() {
				return eventCategoriesDao.findAll();
			}
		};
	}

}
